package com.echo.yixiaowu.main

import android.util.Log
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionLayout
import androidx.compose.animation.core.tween
import androidx.compose.animation.scaleIn
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import com.echo.yixiaowu.feature.main.MainTabsScreen
import com.echo.yixiaowu.feature.main.RemoteImageGallery
import com.echo.yixiaowu.feature.player.MediaPage
import com.echo.yixiaowu.route.AppRoute
import com.echo.yixiaowu.util.NavLock
import com.echo.yixiaowu.util.NavigationHelper
import com.echo.yixiaowu.util.SystemUtil.setStatusBarTheme
import kotlinx.coroutines.delay

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun MainContent(
    navController: NavHostController = rememberNavController()
) {
    val animationSpec = tween<IntOffset>(durationMillis = 350)
    val fadeSpec = tween<Float>(durationMillis = 350)
    val currentBackStackState by navController.currentBackStackEntryAsState()
    val activity = LocalContext.current.findActivity()
    val navigationHelper = remember { NavigationHelper(navController) }

    LaunchedEffect(currentBackStackState) {
        // 在目标路由改变后解锁，避免多次点击跳转
        if (NavLock.getLock()) {
            delay(animationSpec.durationMillis.toLong())
            NavLock.setClip(0)
            NavLock.unlock()
        }
    }

    SideEffect {
        if (activity != null) {
            val route = currentBackStackState?.destination?.hasRoute<AppRoute.Detail>()
            if (route == true) {
                setStatusBarTheme(activity, false)
            } else {
                setStatusBarTheme(activity, true)
            }

        }
    }

    SharedTransitionLayout(modifier = Modifier.fillMaxSize()) {
        NavHost(
            navController = navController,
            startDestination = AppRoute.MainTabs,
        ) {
            composable<AppRoute.MainTabs> {
                MainTabsScreen(
                    navController,
                    sharedTransitionScope = this@SharedTransitionLayout,
                    animatedVisibilityScope = this,
                    navigationHelper = navigationHelper
                )
            }

            composable<AppRoute.Gallery> {
                RemoteImageGallery(
                    param = it.toRoute(),
                    sharedTransitionScope = this@SharedTransitionLayout,
                    animatedVisibilityScope = this,
                    onDismiss = { navigationHelper.popBackStack() }
                )
            }

            composable<AppRoute.Detail>(
                // 进入详情页的动画
                enterTransition = {
                    slideInHorizontally(
                        // 从右侧完全滑入
                        initialOffsetX = { it }, animationSpec = animationSpec
                    ) + scaleIn(
                        initialScale = 1f, animationSpec = fadeSpec
                    )
                },
                // 从详情页返回时，详情页的退出动画
                popExitTransition = {
                    // 从当前位置到targetOffsetX位置
                    slideOutHorizontally(
                        // 向右侧完全滑出
                        targetOffsetX = { it }, animationSpec = animationSpec
                    ).apply {
                        NavLock.setClip(42)
                    }
                }) {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .clip(
                            RoundedCornerShape(
                                topStart = NavLock.getClip().dp, bottomStart = NavLock.getClip().dp
                            )
                        ) // 在父级容器上进行裁剪
                ) {
                    MediaPage(param = it.toRoute(), navController)
                }
            }
        }
        // 添加一个透明的覆盖层来拦截所有点击事件,避免多次导航
        if (NavLock.getLock()) {
            Box(modifier = Modifier
                .fillMaxSize()
                .clickable(enabled = false) {})
        }
    }
}

@Composable
fun MinePage(modifier: Modifier = Modifier) {
    Box(
        modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center
    ) {
        Text("我的", fontSize = 20.sp)
    }
}
